<template>
	<div class="system-user-container layout-padding">
		<el-card shadow="hover" class="layout-padding-auto">
			<div class="system-user-search mb15">
				<el-input size="default" placeholder="请输入商品名称" style="max-width: 180px" v-model="un"> </el-input>
				<el-button size="default" type="primary" class="ml10" @click="queryUser()">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
				<el-button size="default" type="success" class="ml10" @click="onOpenAddUser('add')">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增价格数据
				</el-button>
			</div>
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
				<el-table-column type="index" label="序号" width="60" />
				<!-- <el-table-column prop="priceId" label="编号" show-overflow-tooltip width="60"></el-table-column> -->
				<el-table-column prop="goods" label="商品名称" show-overflow-tooltip></el-table-column>
				<el-table-column prop="goodsId" label="商品编号" show-overflow-tooltip></el-table-column>
				<el-table-column prop="price" label="价格（kg/元）" show-overflow-tooltip></el-table-column>
				<el-table-column prop="county" label="城市" show-overflow-tooltip></el-table-column>
				<el-table-column prop="provider" label="供应商" show-overflow-tooltip></el-table-column>
                <el-table-column prop="date" label="发布时间" show-overflow-tooltip></el-table-column>
                <el-table-column prop="type" label="类型" show-overflow-tooltip></el-table-column>
				<el-table-column prop="status" label="数据状态" show-overflow-tooltip >
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.status">启用</el-tag>
						<el-tag type="info" v-else>禁用</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="100">
					<template #default="scope">
						<el-button :disabled="scope.row.userName === ''" size="small" text type="primary" @click="onOpenEditUser('edit', scope.row)"
							>修改</el-button
						>

						<el-button size="small" text type="primary" @click="onRowDel(scope.row)" v-if="scope.row.status">删除</el-button>
						<el-button size="small" text type="primary" @click="onEnable(scope.row)" v-else>重启</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[100, 200, 300]"
				v-model:current-page="state.tableData.param.pageNum"
				background
				v-model:page-size="state.tableData.param.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="state.tableData.total"
			>
			</el-pagination>
		</el-card>
		<UserDialog ref="userDialogRef" @refresh="getTableData()" />
	</div>
</template>

<script setup name="systemUser">
import { ElMessageBox, ElMessage } from 'element-plus';
import { onMounted } from 'vue';
import axios from 'axios';
import api from "/@/api.js";
// 引入组件
const UserDialog = defineAsyncComponent(() => import('/@/views/data/price/dialog.vue'));

// 定义变量内容
const userDialogRef = ref();
const state = reactive({
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
			pageNum: 1,
			pageSize: 100,
		},
	},
});



// 初始化表格数据
const getTableData = () => {
	/*userApi().getAllUserByPage({"page":1,"pageSize":10}).then(res=>{}
	).catch(function(response){
		console.log("请求发送成功");
		console.log(response);
	}
	).finally(function(){
		console.log("2");
	}
		
	);*/

	axios({
		method: 'post',
		url: api.commonUrl + '/priceData/getallprice',
		// data: {
		// 	page: 1,
		// 	pageSize: 10,
		// },
		data: `page=` + state.tableData.param.pageNum + `&pageSize=` + state.tableData.param.pageSize,
	}).then(function (response) {
			if(response.data.code === 200){
				state.tableData.loading = true;
			const data = [];
			for (let i = 0; i < response.data.data.records.length; i++) {
				//console.log(user);
				data.push({
					priceId: response.data.data.records[i].priceId,
					goods: response.data.data.records[i].goods,
                    goodsId: response.data.data.records[i].goodsId,
                    price: response.data.data.records[i].price,
                    county: response.data.data.records[i].county,
                    provider: response.data.data.records[i].provider,
                    date: response.data.data.records[i].date,
                    type: response.data.data.records[i].type,
					status: response.data.data.records[i].status === '1' ? true : false,
					
				});
			}
			state.tableData.data = data;
			state.tableData.total = response.data.data.total;
			setTimeout(() => {
				state.tableData.loading = false;
			}, 500);
		}else{
			ElMessage.error(response.data.message);
		}
	});
};

let un = ref("");

// 根据用户名查询用户
const queryUser = () => {
	//console.log(un);
	if(un.value === ""){
		ElMessage.error("请输入商品名称");
		return;
	}

	axios({
		method: 'get',
		url: api.commonUrl + '/priceData/selectbygoods?goods=' + un.value,
	}).then(function (response) {
		if(response.data.code === 200){
			state.tableData.loading = true;
			const data = [];
			for (let i = 0; i < response.data.data.userList.length; i++) {
				var user = response.data.data.userList[i];
				//console.log(user);
				data.push({
					priceId: response.data.data.records[i].priceId,
					goods: response.data.data.records[i].goods,
                    goodsId: response.data.data.records[i].goodsId,
                    price: response.data.data.records[i].price,
                    county: response.data.data.records[i].county,
                    provider: response.data.data.records[i].provider,
                    date: response.data.data.records[i].date,
                    type: response.data.data.records[i].type,
					status: response.data.data.records[i].status === '1' ? true : false,
					
				});
			}
			state.tableData.data = data;
			//console.log(response.data.data.total);
			state.tableData.total = response.data.data.total;

			setTimeout(() => {
				state.tableData.loading = false;
			}, 500);
		}else{
			ElMessage.error(response.data.message);
		}
		
	});
	un.value = '';
};
// 打开新增用户弹窗
const onOpenAddUser = (type) => {
	userDialogRef.value.openDialog(type);
	
};
// 打开修改用户弹窗
const onOpenEditUser = (type, row) => {
	userDialogRef.value.openDialog(type, row);
};
// 删除用户
const onRowDel = (row) => {
	//alert(row);
	ElMessageBox.confirm(`此操作将删除此纪录，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
			axios({
				method: 'post',
				url: api.commonUrl + '/priceData/deletepricedatabyid',
				data: `id=` + row.priceId,
			}).then(function (response) {
				if (response.data.code == 200) {
					getTableData();
					ElMessage.success(response.data.message);
				} else {
					ElMessage.error(response.data.message);
				}
			});
		})
		.catch(() => {});
};

// 重启用户
const onEnable = (row) => {
	//alert(row);
	ElMessageBox.confirm(`此操作将重启此纪录，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
			axios({
				method: 'post',
				url: api.commonUrl + '/priceData/restartpricedatabyid',
				data: `id=` + row.priceId,
			}).then(function (response) {
				if (response.data.code == 200) {
					getTableData();
					ElMessage.success(response.data.message);
				} else {
					ElMessage.error(response.data.message);
				}
			});
		})
		.catch(() => {});
};
// 分页改变
const onHandleSizeChange = (val) => {
	state.tableData.param.pageSize = val;
	getTableData();
};
// 分页改变
const onHandleCurrentChange = (val) => {
	state.tableData.param.pageNum = val;
	getTableData();
};
// 页面加载时
onMounted(() => {
	getTableData();
});
</script>

<style scoped lang="scss">
.system-user-container {
	:deep(.el-card__body) {
		display: flex;
		flex-direction: column;
		flex: 1;
		overflow: auto;
		.el-table {
			flex: 1;
		}
	}
}
</style>
